package com.morningstar.qs.api.basic.common.encrypt.des;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.SecureRandom;

import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.SecretKey;

public class SecUtil {
	private static final String DES = "DES";
	public static byte[] decrypt(SecretKey key,byte[] source) throws Exception{
		SecureRandom sr = new SecureRandom();  
        Cipher cipher = Cipher.getInstance(DES);  
        cipher.init(Cipher.DECRYPT_MODE, key , sr);  
        return cipher.doFinal(source);  
	}
	public static String decrypt(SecretKey key,String source) throws Exception{
		return new String(decrypt(key,hex2byte(source.getBytes())));
	}
	private static String byte2hex(byte[] b) {  
        String hs = "";  
        String stmp = "";  
        for (int n = 0; n < b.length; n++) {  
            stmp = (java.lang.Integer.toHexString(b[n] & 0XFF));  
            if (stmp.length() == 1){  
                hs = hs + "0" + stmp;  
            }else{  
                hs = hs + stmp;  
            }  
        }  
        return hs.toUpperCase();  
    }  
	private static byte[] hex2byte(byte[] b) {  
        if ((b.length % 2) != 0)  
            throw new IllegalArgumentException("even length");  
        byte[] b2 = new byte[b.length / 2];  
        for (int n = 0; n < b.length; n += 2) {  
            String item = new String(b, n, 2);  
            b2[n / 2] = (byte) Integer.parseInt(item, 16);  
        }  
        return b2;  
    }  
	public static void decrypt(SecretKey key,String source,String dest){
		Cipher cipher = null;
		try{
			cipher = Cipher.getInstance(DES);
			cipher.init(Cipher.DECRYPT_MODE, key);
		}catch(Exception e){
			throw new RuntimeException(e);
		} 
		File file = new File(source);
		//String filename = file.getName();
		try{
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
			CipherInputStream in = new CipherInputStream(new BufferedInputStream(new FileInputStream(file)), cipher);
			int thebyte=0;
			while((thebyte = in.read())!=-1){
				out.write(thebyte);
			}
			in.close();
			out.close();
		}catch(Exception e){
			throw new RuntimeException(e);
		}
	}
	public static byte[] encrypt(SecretKey key,byte[] source) throws Exception{  
		SecureRandom sr = new SecureRandom();  
        Cipher cipher = Cipher.getInstance(DES);  
        cipher.init(Cipher.ENCRYPT_MODE, key, sr);
        return cipher.doFinal(source);  
	}
	public static String encrypt(SecretKey key, String source) throws Exception{
		return byte2hex(encrypt(key,source.getBytes()));
	}
	public static void encrypt(SecretKey key,String source,String dest){
	    Cipher cipher = null;
	    try {
	    	cipher = Cipher.getInstance(DES);
	    	cipher.init(Cipher.ENCRYPT_MODE, key);
	    }
	    catch (Exception e) {
	    	throw new RuntimeException(e);
	    }
	    File file = new File(source);
	    //String filename = file.getName();
	    try {
			BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(dest));
			CipherInputStream in = new CipherInputStream(new BufferedInputStream(
					new FileInputStream(file)), cipher);
			int thebyte = 0;
			while ( (thebyte = in.read()) != -1) {
				out.write(thebyte);
			}
			in.close();
			out.close();
	    }
	    catch (Exception e) {
	    	throw new RuntimeException(e);
	    }
	}
}
